const Sequelize = require('sequelize');
const logger = require('./logger.lib');
const environment = require('../config/environment.config');
const db = environment.DB;
const Op = Sequelize.Op;

Object.assign(db.options, {
    logging: (sql) => {
        logger.sql.debug(sql);
    },
    operatorsAliases: {
        $eq: Op.eq,
        $ne: Op.ne,
        $gte: Op.gte,
        $gt: Op.gt,
        $lte: Op.lte,
        $lt: Op.lt,
        $not: Op.not,
        $in: Op.in,
        $notIn: Op.notIn,
        $is: Op.is,
        $like: Op.like,
        $notLike: Op.notLike,
        $iLike: Op.iLike,
        $notILike: Op.notILike,
        $regexp: Op.regexp,
        $notRegexp: Op.notRegexp,
        $iRegexp: Op.iRegexp,
        $notIRegexp: Op.notIRegexp,
        $between: Op.between,
        $notBetween: Op.notBetween,
        $overlap: Op.overlap,
        $contains: Op.contains,
        $contained: Op.contained,
        $adjacent: Op.adjacent,
        $strictLeft: Op.strictLeft,
        $strictRight: Op.strictRight,
        $noExtendRight: Op.noExtendRight,
        $noExtendLeft: Op.noExtendLeft,
        $and: Op.and,
        $or: Op.or,
        $any: Op.any,
        $all: Op.all,
        $values: Op.values,
        $col: Op.col
    },
    query: {
        raw: true  //直接返回数据对象
    }
});

const sequelize = new Sequelize(db.database, db.username,
    db.password, db.options);

sequelize.authenticate().then(() => { console.log('connection successfully') })
    .catch(err => {
        console.error('Unable to connect to the database:', err);
    });

module.exports = sequelize;